package com.example.deviceinfo.SDCardRemove;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.deviceinfo.R;

/**
 * Created by sunny on 15-8-11.
 */
public class SDCard extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdcard_remove);
        register();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sdcardReceiver != null) {
            unregisterReceiver(sdcardReceiver);
            sdcardReceiver = null;
        }

    }

    private void register() {
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        filter2.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        registerReceiver(sdcardReceiver, filter2);
    }

    private BroadcastReceiver sdcardReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, final Intent intent) {
            String action = intent.getAction();
            final String[] packages = intent
                    .getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
            if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE)) {
                if (packages.length > 0) {
                    for (String pkg : packages) {
                        Log.d("test", "sdcardReceiver  pkg:" + pkg);
                    }
                }
                Toast.makeText(SDCard.this, "SDCard Mount", Toast.LENGTH_LONG).show();
            } else if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
                Toast.makeText(SDCard.this, "SDCard UnMount", Toast.LENGTH_LONG).show();
            }
        }
    };

}
